package site.qjdb.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import site.qjdb.constant.Info;
import site.qjdb.pojo.User;
import site.qjdb.pojo.response.CommonResult;
import site.qjdb.pojo.response.ResponseCode;
import site.qjdb.service.ChatService;
import site.qjdb.service.UserService;
import site.qjdb.util.CommonUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/chat")
public class ChatController {
	@Resource
	private ChatService chatService;
	@Resource
	private UserService userService;
 
	/**
	 * 	查询新消息数量
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "/count", method = RequestMethod.POST)
	public Object count(HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if (i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		return new CommonResult<>(ResponseCode.SUCCESS,"success",chatService.findNewCount(((User)session.getAttribute(Info.USER_SESSION)).getUid()));
	}
	
	/**
	 * 	查询最近聊天记录
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "/findRecent", method = RequestMethod.POST)
	public Object findRecent(HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if (i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		return new CommonResult<>(ResponseCode.SUCCESS, chatService.findRecent(((User)session.getAttribute(Info.USER_SESSION)).getUid()));
	}

	/**
	 * 	清除最新消息
	 * 	notMe：聊天对象id
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "/clearNew", method = RequestMethod.POST)
	public Object clearNew(@RequestParam("id") String notMeStr,
			HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if (i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		try {
			Integer notMe = Integer.parseInt(notMeStr);
			chatService.clearNew(((User)session.getAttribute(Info.USER_SESSION)).getUid(), notMe);
			return new CommonResult<>(ResponseCode.SUCCESS);
		} catch(Exception e) {

			return new CommonResult<>(ResponseCode.INCORECT_FORMAT);
		}
	}
	
	/**
	 * 	清除最近聊天
	 * 	notMe：聊天对象id
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "/clearRecent", method = RequestMethod.POST)
	public Object clearRecent(@RequestParam("id") String notMeStr,
			HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if (i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		try {
			Integer notMe = Integer.parseInt(notMeStr);
			chatService.clearRecent(((User)session.getAttribute(Info.USER_SESSION)).getUid(), notMe);
			return new CommonResult<>(ResponseCode.SUCCESS);
		} catch(Exception e) {
			return new CommonResult<>(ResponseCode.INCORECT_FORMAT);
		}
	}
	
	/**
	 * 	清除最近聊天
	 * 	notMe：聊天对象id
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "/setOpen", method = RequestMethod.POST)
	public Object setOpen(@RequestParam("id") String notMeStr,
			HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if (i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		try {
			Integer notMe = Integer.parseInt(notMeStr);
			chatService.setOpen(((User)session.getAttribute(Info.USER_SESSION)).getUid(), notMe);
			return new CommonResult<>(ResponseCode.SUCCESS);
		} catch(Exception e) {
			return new CommonResult<>(ResponseCode.INCORECT_FORMAT);
		}
	}
}
